if (!jsx3.lang.Class.forName("tibco.admin.enterpriseAssets.userMgt.user.AddGroups")) {
    jsx3.require("jsx3.gui.Dialog")
    
    jsx3.lang.Package.definePackage("tibco.admin.enterpriseAssets.userMgt.user", function (addGroups) {
    });

    /**
	 * Extension of the <code>jsx3.gui.Dialog</code> class, for use in User Management, to add groups to a user.
	 */
    jsx3.lang.Class.defineClass("tibco.admin.enterpriseAssets.userMgt.user.AddGroups",
            jsx3.gui.Dialog,
            null,
            function (addGroups) {

				/**
				 * instance initializer
				 */
                addGroups.prototype.initialize = function() {
                    // set the matrix
                    this.groupRsList = this.getDescendantOfName("mtxSearchRs_24_D_2");
                    this.groupRsList.selectedId = -100;
                    this.mtxGroupsAdd = this.getDescendantOfName("mtxAddGroups_24_D_2");
                    this.searchBtn = this.getDescendantOfName("btnSearch_24_D_2");
                    this.saveBtn = this.getDescendantOfName("btnSaveGroup_24_D_2");
                    var curMe = this;
                    this.searchBtn.callsearchGroups = function() {
                        curMe.callsearchGroups();
                    }
                    this.mtxGroupsAdd.onDelete = function(recordId) {
                        curMe.onDelete(recordId);
                    }
                    this.saveBtn.calladdGroupsForUser = function() {
                        tibco.admin.Main.getServer().getCache().setDocument("groups_to_add_cache_xml", curMe.mtxGroupsAdd.getXML());
                        curMe.calladdGroupsForUser();
                    }

                    if (jsx3.lang.Class.forName("tibco.admin.enterpriseAssets.userMgt.window.Window")) {
                        // set the window instance
                        /*
                        this.container = this.getAncestorOfType("tibco.admin.enterpriseAssets.userMgt.window.Window");
                        if (this.container) {
                            this.setWindowId(this.container.getId());
                            this.userid = this.container.getDescendantsOfType("tibco.admin.enterpriseAssets.userMgt.user.Detail")[0].userId;
                            alert(this.userid)
                        }
                        */
                    }
                }

                 // set the window instance
                addGroups.prototype.setWindow = function(window) {
                    this.container = window
                    this.setWindowId(this.container.getId());
                    this.userid = this.container.getDescendantsOfType("tibco.admin.enterpriseAssets.userMgt.user.Detail")[0].userId;
					this.getDescendantOfName("windowBar").setText(
							tibco.admin.Main.getServer().getDynamicProperty("@@titleAddGroups_24_D_2", this.userid), true);
                    // this.getDescendantOfName("lblUserId_24_D_2").setText(this.userid, true);
                }

                /**
				 *   set the current window ID
				 *   @para strId {string} The window id
				 */
                addGroups.prototype.setWindowId = function(strId) {
                    this.windowId = strId
                }

				/**
				 *   get the current window ID
				 *   @return {string} The window id
				 */
                addGroups.prototype.getWindowId = function() {
                    return this.windowId;
                }

                /**
                 *	 Delete the current record in group added list and add the delete record into the search result list
				 *  @para {string} the record id want to be deleted
                 */
                addGroups.prototype.onDelete = function(recordId) {
                    //jsx3.log("============ addGroups.prototype.onDelete");
                    var transRecord = this.mtxGroupsAdd.getRecordNode(recordId);
                    this.mtxGroupsAdd.deleteRecord(recordId, true);
                    try
                    {
                        this.groupRsList.insertRecordNode(transRecord, "jsxroot", true);
                        this.mtxGroupsAdd.repaint();
                    }
                    catch (ex)
                    {
                        tibco.admin.Main.logException(ex, "Error in tibco.admin.enterpriseAssets.userMgt.user.AddGroups.onDelete");
                    }
                    //jsx3.log("--------------- addGroups.prototype.onDelete");
                }



                /**
				 *    Search Groups by keywords	   
				 */
                addGroups.prototype.callsearchGroups = function() {
                    var objService = tibco.admin.Main.loadService("tibco.admin", "tibco/admin/enterpriseAssets/userMgt/window/user/rules/searchGroups.xml", "searchGroups", "Search Groups");
                    objService.subscribe(jsx3.net.Service.ON_SUCCESS, this, this.onsearchGroupsSuccess);
                    //objService.subscribe(jsx3.net.Service.ON_ERROR, this, this.onsearchGroupsError);
                    //objService.subscribe(jsx3.net.Service.ON_INVALID, this, this.onsearchGroupsInvalid);
                    objService.searchLimit = 500;
                    objService.setEndpointURL(tibco.admin.util.ServiceHelper.getUserMgtEndpointURL());
                    objService.doCall();
                };
				
                addGroups.prototype.onsearchGroupsSuccess = function(objEvent) {
                    var sourceXML = objEvent.target.getInboundDocument();
                    this.groupRsList.setSourceXML(sourceXML);
                    var xmldoc = this.groupRsList.getXML();
                    var disabledRecords = xmldoc.selectNodes("//record[@canEditMembers='false']");
                    for (var i = 0; i < disabledRecords.size(); i++)
                    {
                        disabledRecords.get(i).setAttribute("jsxstyle", "color:#cccccc;");
                    }
                    this.groupRsList.repaint();
                };

                addGroups.prototype.onsearchGroupsError = function(objEvent) {
                    var myStatus = objEvent.target.getRequest().getStatus();
                    tibco.admin.util.Log.USERMGT.error("Error, The service call failed. The HTTP Status code is: " + myStatus);
                };

                addGroups.prototype.onsearchGroupsInvalid = function(objEvent) {
                    tibco.admin.util.Log.USERMGT.error("Invalid, The following message node just failed validation:\n\n" + objEvent.message);
                };

                //==================== do save the Groups ====================
                /**
				 *   Add groups for user
				 */
                addGroups.prototype.calladdGroupsForUser = function() {
                    var objService = tibco.admin.Main.loadService("tibco.admin", "tibco/admin/enterpriseAssets/userMgt/window/user/rules/addGroupsForUser.xml", "addGroupsForUser", "Add Groups for User");

                    //subscribe and call
                    objService.subscribe(jsx3.net.Service.ON_SUCCESS, this, this.onaddGroupsForUserSuccess);
                    //objService.subscribe(jsx3.net.Service.ON_ERROR, this, this.onaddGroupsForUserError);
                    //objService.subscribe(jsx3.net.Service.ON_INVALID, this, this.onaddGroupsForUserInvalid);
                    objService.userid = this.userid;
                    objService.setEndpointURL(tibco.admin.util.ServiceHelper.getUserMgtEndpointURL());

                    objService.doCall();
                };

                addGroups.prototype.onaddGroupsForUserSuccess = function(objEvent) {
                    if (objEvent.target.simpleActionResponse == "true") {
                        // Publish a message to window
                        var msg = new Object();
                        msg.subject = tibco.admin.enterpriseAssets.userMgt.EventID.ADD_ITEM_SUCCESS
                        msg.windowId = this.getWindowId();
                        msg.username = objEvent.target.userid;
                        tibco.admin.PubSub.publish(msg);
                        this.doClose();
                    }
                    else {
                        tibco.admin.util.Log.USERMGT.error("Error", "simple action response is " + objEvent.target.simpleActionResponse);
                    }

                };

                addGroups.prototype.onaddGroupsForUserError = function(objEvent) {
                    var myStatus = objEvent.target.getRequest().getStatus();
                    tibco.admin.util.Log.USERMGT.error("Error, The service call failed. The HTTP Status code is: " + myStatus);
                };

                addGroups.prototype.onaddGroupsForUserInvalid = function(objEvent) {
                    tibco.admin.util.Log.USERMGT.error("Invalid, The following message node just failed validation:\n\n" + objEvent.message);
                };
                //====================================================================


            });
}